<!--项目分配人员列表-->
<template>
  <div class="app-container horizontal">
    <!-- 搜索查询 -->
    <div class="form-container">
      <el-form ref="queryForm" :inline="true" label-width="80px" :model="filters">
        <el-form-item label="人员姓名" prop="userName">
          <el-input
            v-model="filters.userName"
            size="small"
            placeholder
            clearable
          />
        </el-form-item>
        <el-form-item label="所属企业" prop="enterpriseName">
          <el-input
            v-model="filters.enterpriseName"
            size="small"
            placeholder
            clearable
          />
        </el-form-item>
        <el-form-item label="创建时间">
          <el-date-picker
            v-model="dateRange"
            size="small"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :clearable="true"
          />
        </el-form-item>
      </el-form>
    </div>
    <!--操作按钮-->
    <div class="btns-group">
      <el-button v-clickForbidden icon="el-icon-search" size="middle" @click="handleQuery()">搜索</el-button>
      <el-button v-clickForbidden icon="el-icon-refresh" size="middle" @click="resetQuery()">重置</el-button>
      <el-button v-if="status !== '2'" icon="el-icon-plus" size="middle" @click="personalAdd()">新增</el-button>
      <el-button v-if="status !== '2'" icon="el-icon-user" size="middle" :disabled="single" @click="handleDelete()">取消分配人员</el-button>
      <el-button icon="el-icon-aim" size="middle" :disabled="multipleSelection.length !== 1" @click="handleExploration">分配勘探点</el-button>
      <span class="personal-type">
        <span>{{ proName }} — {{ personTypeName }}</span>
        <el-button icon="el-icon-arrow-left" size="middle" @click="handleReturn()">返回</el-button>
      </span>
    </div>
    <!-- 表格展示start -->
    <el-table
      ref="multipleTable"
      v-loading="loading"
      :data="tableData"
      :row-style="rowClass"
      style="width: 100%;"
      stripe
      class="table-list"
      :class="highLevelSet===true ? (needTagsView===true ? 'el-body-page-roll-tag-height' : 'el-body-page-roll-height') :
        (needTagsView===true ? 'el-body-page-roll-tag' : 'el-body-page-roll')"
      @selection-change="handleSelectionChange"
      @row-click="rowChecked"
    >
      <el-table-column type="selection" width="38" align="center" />
      <el-table-column type="index" label="序号" width="51" align="center" />
      <el-table-column
        prop="userName"
        label="人员姓名"
        min-width="60"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        prop="idCard"
        label="身份证号"
        min-width="120"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        prop="enterpriseName"
        label="所属企业"
        min-width="200"
        align="center"
        :show-overflow-tooltip="true"
      />
    </el-table>
    <!-- 分页-->
    <pagination
      :total="total"
      :page.sync="filters.pageNum"
      :limit.sync="filters.pageSize"
      @pagination="getList"
    />
    <!-- 表格展示end-->

    <!-- 人员选择组件 -->
    <userTemplate ref="userTemplate" :get-person-list="listProUserNotPermission" @fatherMethod="fatherMethod" />
  </div>
</template>

<script>
// 页面接口
import { listProUser, listProUserAdd, listProUserNotPermission, deleteProUser } from '@/api/projectManage/ProjectAllocation/proPersonnel'
// 页面组件
// 人员选择组件
import userTemplate from '@/components/basicinfo/userTemplate/index'
import { mapState } from 'vuex'
export default {
  components: {
    userTemplate
  },
  data() {
    return {
      listProUserNotPermission, // 分配人员弹层接口
      proName: '', // 项目名称
      personTypeName: '', // 项目类型名称
      /* 搜索条件 start*/
      highLevelSet: false, // 高级设置显示隐藏
      filters: { // 查询参数
        pageNum: 1,
        pageSize: this.fristPageSize,
        proId: '', // 项目id
        userName: '', // 人员姓名
        enterpriseName: '', // 所属企业
        sysUserType: '' // 分配人员类型
      },
      dateRange: [], // 日期范围
      /* 搜索条件 end*/

      /* 列表 start*/
      total: 0, // 分页总条数
      single: true, // 非单个禁用
      selectRow: [], // 高亮样式
      multipleSelection: [], // 选中表格行
      loading: false, // 列表加载
      tableData: [], // 列表数据
      /* 列表 end*/
      userId: '', // 选择用户
      addUserVisible: false,
      status: ''// 项目状态
    }
  },
  computed: {
    ...mapState({
      needTagsView: state => state.settings.tagsView,
      personTypeList: state => state.basicInfo.personType // 人员类型
    }),
    // 高级搜索显示隐藏
    handleTagShow() {
      return this.highLevelSet
    }
  },
  watch: {
  },
  mounted() {
    this.filters.sysUserType = this.$route.query.personType || ''
    this.filters.proId = this.$store.state.basicInfo.proId
    this.proName = this.$route.query.proName
    this.personTypeName = this.$route.query.personTypeName
    this.status = this.$store.state.basicInfo.proStatus
    this.getList()
  },
  methods: {
    /* 列表 start*/
    // 单击每行时每行的复选框选中
    rowChecked(row) {
      const selectData = this.multipleSelection
      if (selectData.length > 0) {
        selectData.forEach(item => {	// 判断 如果当前的一行被勾选, 再次点击的时候就会取消选中
          if (item === row) {
            this.$refs.multipleTable.toggleRowSelection(row, false)
          } else {
            this.$refs.multipleTable.toggleRowSelection(row, true)
          }
        })
      } else {
        this.$refs.multipleTable.toggleRowSelection(row, true)
      }
    },
    // 点击表格前面选框时触发的事件
    handleSelectionChange(val) {
      if (JSON.stringify(val[val.length - 1]) === '{}') {
        this.$refs.multipleTable.toggleRowSelection(val.pop(), false)
        return
      }
      this.single = val.length === 0
      this.multipleSelection = val
      this.selectRow = []
      if (this.multipleSelection.length > 0) {
        this.multipleSelection.forEach((item, index) => {
          this.selectRow.push(this.tableData.indexOf(item))
        })
      }
    },
    // 高亮样式
    rowClass({ row, rowIndex }) {
      if (this.selectRow.includes(rowIndex)) {
        return { 'background-color': 'rgba(185,221,249,.75)' }
      }
    },
    // 获取列表
    async getList() {
      this.loading = true
      try {
        const { data: { rows, total }} = await listProUser(this.changeDateRange(this.filters, this.dateRange))
        this.loading = false
        this.tableData = rows
        this.total = total
        // this.addTaleData()
      } catch {
        this.loading = false
      }
    },
    // list表格填充空白行
    addTaleData() {
      if (this.tableData.length < this.fristPageSize) {
        const arr = {}
        this.tableData.push(arr)
        this.addTaleData()
      }
    },
    // 搜索方法
    handleQuery() {
      this.filters.pageNum = 1
      this.getList()
    },
    // 重置搜索
    resetQuery() {
      this.resetForm('queryForm')
      this.dateRange = []
      this.handleQuery()
    },
    // 删除
    handleDelete() {
      // 获取选中行的id
      const child = this.multipleSelection
      const { proId } = this.filters
      if (child.length === 0) {
        this.msgInfo('请选择需要取消分配的人员!')
      } else {
        this.$confirm('确认要取消分配此人员吗?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async() => {
            this.loading = true
            const removeArr = child.map(item => {
              return { id: item.id, sysUserType: item.sysUserType, proId }
            })
            await deleteProUser(removeArr)
            this.loading = false
            this.msgSuccess('取消分配成功！')
            // 重新加载表格数据
            this.getList()
          })
          .catch(() => {
          })
      }
    },
    /* 列表 end*/

    /* 新增人员 start*/
    async fatherMethod(id, row, name) {
      const { proId } = this.filters
      if (row.length > 0) {
        const newArr = row.map(item => {
          const obj = {}
          for (const k in item) {
            item[k] && (obj[k] = item[k])
          }
          return { ...obj, proId }
        })
        await listProUserAdd(newArr)
        this.msgSuccess('保存成功')
        this.resetQuery()
      }
    },
    personalAdd() {
      this.childrenTitle = `请选择` + this.personTypeName
      if (this.multipleSelection[0]) {
        this.$refs.userTemplate.handleAdd(this.childrenTitle, '1', this.filters.sysUserType, this.filters.proId, '', this.multipleSelection[0].personId)
      } else {
        this.$refs.userTemplate.handleAdd(this.childrenTitle, '1', this.filters.sysUserType, this.filters.proId, '')
      }
    },
    /* 新增人员 end*/

    // 项目人员勘探点
    handleExploration() {
      const { id, userName, sysUserType } = this.multipleSelection[0]
      this.$router.push({
        path: '/newProject/project-proPersonalExploration',
        name: '项目人员已配勘探点',
        query: { personId: id, personName: userName, personType: sysUserType }
      })
    },

    // 返回
    handleReturn() {
      this.$router.push({
        path: '/newProject/distributionList'
      })
    }
  }
}
</script>

